//
//  ShowTableViewController.m
//  DeliciousFood1
//
//  Created by brother on 15/10/20.
//  Copyright © 2015年 com.hxm. All rights reserved.
//

#import "ShowTableViewController.h"
#import "AppDelegate.h"
#define HeadUrl @"http://localhost:8888/head/"
#import "User.h"
#import "UIImage+CLImageCircle.h"
#import <AFNetworking.h>
#import "GetCommentsViewController.h"
#import "MyLoginViewController.h"

@interface ShowTableViewController ()

@property (weak, nonatomic) IBOutlet UILabel *foodName;

@property (weak, nonatomic) IBOutlet UILabel *despretion;
@property (weak, nonatomic) IBOutlet UILabel *name;
@property (weak, nonatomic) IBOutlet UIImageView *picture;
@property (weak, nonatomic) IBOutlet UIImageView *head;
@property (nonatomic,strong)NSArray *array;
@property (weak, nonatomic) IBOutlet UINavigationItem *topName;


- (IBAction)back:(id)sender;
@end

@implementation ShowTableViewController

- (void)viewDidLoad
{
//    self.picture.image = [UIImage imageNamed:self.dic[@"albums"]];
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.dic[@"albums"]]];
    self.picture.image = [UIImage imageWithData:data];
    self.foodName.text = self.dic[@"title"];
    self.topName.title = self.dic[@"title"];
    self.despretion.text = self.dic[@"intro"];
    
    AppDelegate *application =  [[UIApplication sharedApplication] delegate] ;
    if (!application.user) {
       UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"登录"style:UIBarButtonItemStyleDone target:self action:@selector(myLogin)];

        
        [self.topName setRightBarButtonItem:rightButton];
    }else
    {
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",HeadUrl,application.user.head]]];
        NSLog(@"%@",[NSString stringWithFormat:@"%@%@",HeadUrl,application.user.head]);
        self.head.image = [UIImage circleImageWithOldImage:[UIImage imageWithData:data] WithBorder:0 WithColor:nil];
        self.name.text = application.user.username;
        NSLog(@"%@",application.user.username);
    }
}

- (void)myLogin
{
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    MyLoginViewController *myLogn = [storyBoard instantiateViewControllerWithIdentifier:@"myLogin"];
    [self presentViewController:myLogn animated:YES completion:nil];
}

//懒加载
- (NSArray *)array
{
    if (_array == nil) {
        _array = self.dic[@"steps"];
    }
    return _array;
}

- (IBAction)back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    }
    NSDictionary *dic = self.array[indexPath.row];
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:dic[@"img"]]];
    cell.imageView.image = [UIImage imageWithData:data];
    cell.textLabel.text = dic[@"step"];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}

//喜欢菜谱
- (IBAction)like:(id)sender {
    AppDelegate *application =  [[UIApplication sharedApplication] delegate] ;
    if (!application.user) {
        [[[UIAlertView alloc] initWithTitle:@"提示" message:@"请先登录才能添加喜欢哦！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
    }else
    {
        //查询菜谱是否已经存在，不存在就添加，存在就进一步添加喜欢
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        
        NSString *url = @"http://localhost:8888/thinkphp/index.php/home/food/findFood";
        
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        dic[@"name"] = self.dic[@"title"];
        
        [manager GET:url parameters:dic success:^void(AFHTTPRequestOperation * operation, id  responseObject) {
            if ([(NSArray *)responseObject count] == 0) {
                NSLog(@"没有");
                //添加这道菜
                NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
                dic1[@"name"] = self.dic[@"title"];
                dic1[@"description"] = self.dic[@"intro"];
                dic1[@"picture"] = self.dic[@"albums"];
                NSString *url1 = @"http://localhost:8888/thinkphp/index.php/home/food/addFood";
                [manager GET:url1 parameters:dic1 success:^(AFHTTPRequestOperation * operation, id  responseObject) {
                    //添加食谱的具体步骤
                    NSString *url2 = @"http://localhost:8888/thinkphp/index.php/home/food/addFoodStep";
                    NSMutableDictionary *dic2 = [NSMutableDictionary dictionary];
                    for (NSDictionary *dict in self.array) {
                        dic2[@"foodid"] = (NSDictionary *)responseObject[0][@"id"];
                        dic2[@"picture"] = dict[@"img"];
                        dic2[@"description"] = dict[@"step"];
                        [manager GET:url2 parameters:dic2 success:^(AFHTTPRequestOperation *  operation, id  responseObject) {
                            NSLog(@"步骤成功！");
                        } failure:^(AFHTTPRequestOperation * operation, NSError * error) {
                            NSLog(@"步骤失败！");
                        }];
                    }
                    //添加用户喜欢
                    NSString *url3 = @"http://localhost:8888/thinkphp/index.php/home/food/addLikeFood";
                    NSMutableDictionary *dic3 = [NSMutableDictionary dictionary];
                    dic3[@"foodid"] = (NSDictionary *)responseObject[0][@"id"];
                    dic3[@"userid"] = [NSNumber numberWithInt:(application.user.ID)];
                    [manager GET:url3 parameters:dic3 success:^(AFHTTPRequestOperation * operation, id   responseObject) {
                        NSLog(@"ok");
                    } failure:^(AFHTTPRequestOperation *  operation, NSError * error) {
                        NSLog(@"over");
                    }];
                    
                } failure:^(AFHTTPRequestOperation * operation, NSError * error) {
                     NSLog(@"fail");
                }];
            }else
            {
                //添加用户喜欢
                NSString *url3 = @"http://localhost:8888/thinkphp/index.php/home/food/addLikeFood";
                NSMutableDictionary *dic3 = [NSMutableDictionary dictionary];
                dic3[@"foodid"] = (NSDictionary *)responseObject[0][@"id"];
                dic3[@"userid"] = [NSNumber numberWithInt:(application.user.ID)];
                NSLog(@"%@--%@",dic3[@"foodid"],dic3[@"userid"]);
                [manager GET:url3 parameters:dic3 success:^(AFHTTPRequestOperation * operation, id   responseObject) {
                    NSLog(@"ok");
                } failure:^(AFHTTPRequestOperation *  operation, NSError * error) {
                    NSLog(@"over");
                }];
            }
        } failure:^(AFHTTPRequestOperation *  operation, NSError * error) {
            NSLog(@"%@",error);
        }];
        
    }
}
//收藏菜谱
- (IBAction)save:(id)sender {
    AppDelegate *application =  [[UIApplication sharedApplication] delegate] ;
    if (!application.user) {
        [[[UIAlertView alloc] initWithTitle:@"提示" message:@"请先登录才能添加喜欢哦！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
    }else
    {
        //查询菜谱是否已经存在，不存在就添加，存在就进一步添加喜欢
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        
        NSString *url = @"http://localhost:8888/thinkphp/index.php/home/food/findFood";
        
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        dic[@"name"] = self.dic[@"title"];
        
        [manager GET:url parameters:dic success:^void(AFHTTPRequestOperation * operation, id  responseObject) {
            if ([(NSArray *)responseObject count] == 0) {
                NSLog(@"没有");
                //添加这道菜
                NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
                dic1[@"name"] = self.dic[@"title"];
                dic1[@"description"] = self.dic[@"intro"];
                dic1[@"picture"] = self.dic[@"albums"];
                NSString *url1 = @"http://localhost:8888/thinkphp/index.php/home/food/addFood";
                [manager GET:url1 parameters:dic1 success:^(AFHTTPRequestOperation * operation, id  responseObject) {
                    //添加食谱的具体步骤
                    NSString *url2 = @"http://localhost:8888/thinkphp/index.php/home/food/addFoodStep";
                    NSMutableDictionary *dic2 = [NSMutableDictionary dictionary];
                    for (NSDictionary *dict in self.array) {
                        dic2[@"foodid"] = (NSDictionary *)responseObject[0][@"id"];
                        dic2[@"picture"] = dict[@"img"];
                        dic2[@"description"] = dict[@"step"];
                        [manager GET:url2 parameters:dic2 success:^(AFHTTPRequestOperation *  operation, id  responseObject) {
                            NSLog(@"步骤成功！");
                        } failure:^(AFHTTPRequestOperation * operation, NSError * error) {
                            NSLog(@"步骤失败！");
                        }];
                    }
                    //添加用户喜欢
                    NSString *url3 = @"http://localhost:8888/thinkphp/index.php/home/food/addSaveFood";
                    NSMutableDictionary *dic3 = [NSMutableDictionary dictionary];
                    dic3[@"foodid"] = (NSDictionary *)responseObject[0][@"id"];
                    dic3[@"userid"] = [NSNumber numberWithInt:(application.user.ID)];
                    [manager GET:url3 parameters:dic3 success:^(AFHTTPRequestOperation * operation, id   responseObject) {
                        NSLog(@"ok");
                    } failure:^(AFHTTPRequestOperation *  operation, NSError * error) {
                        NSLog(@"over");
                    }];
                    
                } failure:^(AFHTTPRequestOperation * operation, NSError * error) {
                    NSLog(@"fail");
                }];
            }else
            {
                //添加用户喜欢
                NSString *url3 = @"http://localhost:8888/thinkphp/index.php/home/food/addSaveFood";
                NSMutableDictionary *dic3 = [NSMutableDictionary dictionary];
                dic3[@"foodid"] = (NSDictionary *)responseObject[0][@"id"];
                dic3[@"userid"] = [NSNumber numberWithInt:(application.user.ID)];
                [manager GET:url3 parameters:dic3 success:^(AFHTTPRequestOperation * operation, id   responseObject) {
                    NSLog(@"ok");
                } failure:^(AFHTTPRequestOperation *  operation, NSError * error) {
                    NSLog(@"over");
                }];
            }
        } failure:^(AFHTTPRequestOperation *  operation, NSError * error) {
            NSLog(@"%@",error);
        }];
        
    }

}
//评论菜谱
- (IBAction)comment:(id)sender {
    AppDelegate *application =  [[UIApplication sharedApplication] delegate] ;
    if (!application.user) {
        [[[UIAlertView alloc] initWithTitle:@"提示" message:@"请先登录才能添加喜欢哦！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
    }else
    {
        //查询菜谱是否已经存在，不存在就添加，存在就进一步添加喜欢
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        
        NSString *url = @"http://localhost:8888/thinkphp/index.php/home/food/findFood";
        
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        dic[@"name"] = self.dic[@"title"];
        
        [manager GET:url parameters:dic success:^void(AFHTTPRequestOperation * operation, id  responseObject) {
            if ([(NSArray *)responseObject count] == 0) {
                NSLog(@"没有");
                //添加这道菜
                NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
                dic1[@"name"] = self.dic[@"title"];
                dic1[@"description"] = self.dic[@"intro"];
                dic1[@"picture"] = self.dic[@"albums"];
                NSString *url1 = @"http://localhost:8888/thinkphp/index.php/home/food/addFood";
                [manager GET:url1 parameters:dic1 success:^(AFHTTPRequestOperation * operation, id  responseObject) {
                    //添加食谱的具体步骤
                    NSString *url2 = @"http://localhost:8888/thinkphp/index.php/home/food/addFoodStep";
                    NSMutableDictionary *dic2 = [NSMutableDictionary dictionary];
                    for (NSDictionary *dict in self.array) {
                        dic2[@"foodid"] = (NSDictionary *)responseObject[0][@"id"];
                        dic2[@"picture"] = dict[@"img"];
                        dic2[@"description"] = dict[@"step"];
                        [manager GET:url2 parameters:dic2 success:^(AFHTTPRequestOperation *  operation, id  responseObject) {
                            NSLog(@"步骤成功！");
                        } failure:^(AFHTTPRequestOperation * operation, NSError * error) {
                            NSLog(@"步骤失败！");
                        }];
                    }
                    //添加用户评论
                    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                    GetCommentsViewController *getComment = [storyBoard instantiateViewControllerWithIdentifier:@"comment"];
                    getComment.foodid = [((NSDictionary *)responseObject[0])[@"id"] intValue];
                    [self presentViewController:getComment animated:YES completion:nil];
                } failure:^(AFHTTPRequestOperation * operation, NSError * error) {
                    NSLog(@"fail");
                }];
            }else
            {
                //添加用户评论
                UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                GetCommentsViewController *getComment = [storyBoard instantiateViewControllerWithIdentifier:@"comment"];
                getComment.foodid = [((NSDictionary *)responseObject[0])[@"id"] intValue];
                [self presentViewController:getComment animated:YES completion:nil];            }
        } failure:^(AFHTTPRequestOperation *  operation, NSError * error) {
            NSLog(@"%@",error);
        }];
        
    }
}
//分享菜谱
- (IBAction)share:(id)sender {
}
//登录跳转
- (void)login
{
    NSLog(@"走你！");
}


@end
